package ch.elektroplan.mueller.passwordSave.presentation;

import ch.elektroplan.mueller.passwordSave.R;
import ch.elektroplan.mueller.passwordSave.business.supporters.Enigma;
import ch.elektroplan.mueller.passwordSave.business.supporters.Password_DB_Generator;
import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MakeNewUser extends Activity {

	private Button makeUser;
	private EditText name, firstname, userName, ip, password;
	private Password_DB_Generator dbGenerator;
	private SQLiteDatabase database;
	private Enigma enigma;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newuser);

		makeUser = (Button) findViewById(R.id.bu_make);
		
		makeUser.setOnClickListener(new makeUserClicklistener());
		

		name = (EditText) findViewById(R.id.et_enterName);
		firstname = (EditText) findViewById(R.id.et_enterFirstName);
		userName = (EditText) findViewById(R.id.et_enterUsername);
		ip = (EditText) findViewById(R.id.et_enterIP);
		password = (EditText) findViewById(R.id.et_enterPassword);

		dbGenerator = new Password_DB_Generator(this);
		database = dbGenerator.getWritableDatabase();
		
		enigma = new Enigma();
		
		if (database == null) {
			Toast.makeText(MakeNewUser.this, "database is null", Toast.LENGTH_SHORT).show();
			
		}
	}

	private class makeUserClicklistener implements OnClickListener {

		public void onClick(View v) {

			if (simplecheckValues()) {
				makeDBinsert();
				MakeNewUser.this.finish();
			}else{
				
				Toast.makeText(MakeNewUser.this, "some failed value", Toast.LENGTH_SHORT).show();
				
			}
		}

		private void makeDBinsert() {

			try{
				
			ContentValues values = new ContentValues();
			
			values.put(dbGenerator.NAME, enigma.encode(name.getText().toString()));
			values.put(dbGenerator.FIRSTNAME, enigma.encode(firstname.getText().toString()));
			values.put(dbGenerator.IP, enigma.encode(ip.getText().toString()));
			values.put(dbGenerator.USERNAME, enigma.encode(userName.getText().toString()));
			values.put(dbGenerator.PASSWORD, enigma.encode(password.getText().toString()));
						
			database.insert(Password_DB_Generator.USERTABLE, null, values);
			
			Toast.makeText(MakeNewUser.this, "db instert ok", Toast.LENGTH_SHORT).show();

			}catch (Exception e) {
				Toast.makeText(MakeNewUser.this, "db instert failed", Toast.LENGTH_SHORT).show();

				
			}
			
		}

		private boolean simplecheckValues() {

			if(
			valueControll(name.getText().toString(), true) &&
			valueControll(firstname.getText().toString(), true)&&
			valueControll(userName.getText().toString(), false)&&
			valueControll(ip.getText().toString(), true)&&
			valueControll(password.getText().toString(), true)
			){return true;}else{

			return false;
			}
		}

		private boolean valueControll(String value, boolean needed) {

			if (needed) {

				if (value==null) {
					return false;
				}
				if (value.equals("")) {
					return false;
				}
			}
			if (value.length() > 30) {
				return false;
			}

			return true;

		}

	}

}
